home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlb20 / lib / utime.c < prev    next >
C/C++ Source or Header  |  1991-10-19  |  2KB  |  87 lines

  1. /* utime -- set the file modification time of the given file
  2.  * according to the time given; a time of 0 means the current
  3.  * time.
  4.  *
  5.  * stime -- set the current time to the value given.
  6.  *
  7.  * All times are in Unix format, i.e. seconds since to
  8.  * midnight, January 1, 1970 GMT
  9.  *
  10.  * written by Eric R. Smith, and placed in the public domain.
  11.  *
  12.  */
  13.  
  14. #include <limits.h>
  15. #include <sys/types.h>
  16. #include <time.h>
  17. #include <errno.h>
  18. #include <osbind.h>
  19. #include <assert.h>
  20. #include "lib.h"
  21.  
  22. /* convert a Unix time into a DOS time. The longword returned contains
  23.    the time word first, then the date word */
  24.  
  25. time_t dostime(t)
  26.     time_t t;
  27. {
  28.         time_t time, date;
  29.     struct tm *ctm;
  30.  
  31.     if (!(ctm = localtime(&t)))
  32.         return 0;
  33.     time = (ctm->tm_hour << 11) | (ctm->tm_min << 5) | (ctm->tm_sec >> 1);
  34.     date = ((ctm->tm_year - 80) & 0x7f) << 9;
  35.     date |= ((ctm->tm_mon+1) << 5) | (ctm->tm_mday);
  36.     return (time << 16) | date;
  37. }
  38.  
  39. int
  40. utime(_filename, tset)
  41.       const char *_filename;
  42.       const struct utimbuf *tset;
  43. {
  44.     int fh;
  45.     time_t settime;
  46.     unsigned long dtime;    /* dos time equivalent */
  47.     char filename[PATH_MAX];
  48.  
  49.     if (tset)
  50.         settime = tset->modtime;
  51.     else
  52.         time(&settime);
  53.  
  54.     (void)_unx2dos(_filename, filename);
  55.     dtime = dostime(settime);    /* convert unix time to dos */
  56.     fh = Fopen(filename, 2);
  57.     if (fh < 0) {
  58.         errno = -fh;
  59.         return -1;
  60.     }
  61.     (void)Fdatime(&dtime, fh, 1);
  62.     if (fh = Fclose(fh)) {
  63.         errno = -fh;
  64.         return -1;
  65.     }
  66.     return 0;
  67. }
  68.  
  69. int stime(t)
  70.     time_t *t;
  71. {
  72.     unsigned long dtime;
  73.     unsigned date, time;
  74.     int r;
  75.  
  76.     assert(t != 0);
  77.     dtime = dostime(*t);
  78.     date = (dtime & 0xffff);
  79.     time = (dtime >> 16) & 0xffff;
  80.  
  81.     if ((r = Tsetdate(date)) || (r = Tsettime(time))) {
  82.         errno = -r;
  83.         return -1;
  84.     }
  85.     return 0;
  86. }
  87.